{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.globals import set_llm_cache, set_verbose, set_debug\n",
    "from langchain_openai import OpenAI\n",
    "\n",
    "llm = OpenAI(model_name=\"gpt-3.5-turbo-instruct\", n=2, best_of=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 为了保证测试准确，可以先删除已经存在的缓存数据\n",
    "# %rm .langchain.db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.cache import SQLiteCache\n",
    "\n",
    "sqllite_cache = SQLiteCache(database_path=\"./langchain.db\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\web\\langchain\\langchain-study\\.venv\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:139: LangChainDeprecationWarning: The method `BaseLLM.predict` was deprecated in langchain-core 0.1.7 and will be removed in 0.3.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[llm:OpenAI] Entering LLM run with input:\n",
      "\u001b[0m{\n",
      "  \"prompts\": [\n",
      "    \"请给我介绍下AIGC\"\n",
      "  ]\n",
      "}\n",
      "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[llm:OpenAI] [4.24s] Exiting LLM run with output:\n",
      "\u001b[0m{\n",
      "  \"generations\": [\n",
      "    [\n",
      "      {\n",
      "        \"text\": \"\\n\\nAIGC（Artificial Intelligence Global Conference）是由中国人工智能学会主办的年度国际性学术会议，旨在推动人工智能领域的学术交流与合作，促进人工智能技术的创新与应用。AIGC每年都会邀请国内外顶级的学术专家、行业领袖和企业代表，围绕人工智能领域的最新研究成果、技术发展趋势和应用案例进行深入探讨和分享。会议内容涵盖人工智能理论、算法、系统、应用等多个方面，包括机器学习、自然语言处理、图像处理、智能系统、大数据分析等，涉及的行业领域也十分广泛，如金融、医疗、教育、\",\n",
      "        \"generation_info\": {\n",
      "          \"finish_reason\": \"length\",\n",
      "          \"logprobs\": null\n",
      "        },\n",
      "        \"type\": \"Generation\"\n",
      "      },\n",
      "      {\n",
      "        \"text\": \"\\n\\n\\nAIGC（全称为“美国国际集团”）是一家总部位于美国纽约的跨国保险金融服务公司，成立于1919年。旗下拥有多家子公司，业务涵盖财产保险、意外伤害保险、人寿保险、退休计划、投资管理等领域。\\n\\nAIGC是全球最大的保险公司之一，在全球拥有超过80个国家和地区的分支机构和子公司。其主要业务包括财产保险、意外伤害保险、人寿保险和养老金计划。\\n\\nAIGC的主要客户群体包括个人、家庭、中小型企业、大型企业和政府机构。其业务范围涵盖财产保险、责任保险、汽车保险\",\n",
      "        \"generation_info\": {\n",
      "          \"finish_reason\": \"length\",\n",
      "          \"logprobs\": null\n",
      "        },\n",
      "        \"type\": \"Generation\"\n",
      "      }\n",
      "    ]\n",
      "  ],\n",
      "  \"llm_output\": {\n",
      "    \"token_usage\": {\n",
      "      \"prompt_tokens\": 10,\n",
      "      \"completion_tokens\": 512,\n",
      "      \"total_tokens\": 522\n",
      "    },\n",
      "    \"model_name\": \"gpt-3.5-turbo-instruct\"\n",
      "  },\n",
      "  \"run\": null\n",
      "}\n",
      "CPU times: total: 78.1 ms\n",
      "Wall time: 4.38 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n\\nAIGC（Artificial Intelligence Global Conference）是由中国人工智能学会主办的年度国际性学术会议，旨在推动人工智能领域的学术交流与合作，促进人工智能技术的创新与应用。AIGC每年都会邀请国内外顶级的学术专家、行业领袖和企业代表，围绕人工智能领域的最新研究成果、技术发展趋势和应用案例进行深入探讨和分享。会议内容涵盖人工智能理论、算法、系统、应用等多个方面，包括机器学习、自然语言处理、图像处理、智能系统、大数据分析等，涉及的行业领域也十分广泛，如金融、医疗、教育、'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "from langchain.cache import InMemoryCache\n",
    "\n",
    "# set_verbose(True)\n",
    "set_debug(True)\n",
    "\n",
    "# 这里将InMemoryCache替换成了SQLiteCache\n",
    "set_llm_cache(sqllite_cache)\n",
    "\n",
    "llm.predict(\"请给我介绍下AIGC\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 0 ns\n",
      "Wall time: 4.48 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n\\nAIGC（Artificial Intelligence Global Conference）是由中国人工智能学会主办的年度国际性学术会议，旨在推动人工智能领域的学术交流与合作，促进人工智能技术的创新与应用。AIGC每年都会邀请国内外顶级的学术专家、行业领袖和企业代表，围绕人工智能领域的最新研究成果、技术发展趋势和应用案例进行深入探讨和分享。会议内容涵盖人工智能理论、算法、系统、应用等多个方面，包括机器学习、自然语言处理、图像处理、智能系统、大数据分析等，涉及的行业领域也十分广泛，如金融、医疗、教育、'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "llm.predict(\"请给我介绍下AIGC\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
